//
//  ViewController.m
//  MultiSelectPhoto
//
//  Created by Burn on 2019/6/17.
//  Copyright © 2019 menksoft. All rights reserved.
//

#import "ViewController.h"
#import "MultiSelectImageController.h"
#import "PhotoListController.h"
@interface ViewController ()

@property(strong,nonatomic) NSMutableArray *images;
@property (weak, nonatomic) IBOutlet UIImageView *image1;
@property (weak, nonatomic) IBOutlet UIImageView *image2;

@end



@implementation ViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    
    //  访问相册前-执行下面代码-可以直接弹出相册权限请求(第一次安装)
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {

        if (status != PHAuthorizationStatusAuthorized) {
            UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"尚未获取到相册权限" message:@"是否到设置处进行权限设置" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *actionSure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [self openSystemSet];
            }];
            UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                [alter dismissViewControllerAnimated:true completion:nil];
            }];
            [alter addAction:actionSure];
            [alter addAction:actionCancel];
            [self presentViewController:alter animated:true completion:nil];
        }else{
            NSLog(@"获取到权限");
        }
    }];
    
    
    
    self.view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255) / 255.0 green:arc4random_uniform(255) / 255.0 blue:arc4random_uniform(255) / 255.0 alpha:1];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(noti:) name:@"POSTIMAGE" object:nil];
    
    NSString * path =NSHomeDirectory();
    NSString * Pathimg =[path stringByAppendingString:@"/Documents/111.png"];
    [UIImagePNGRepresentation([UIImage imageNamed:@"choose"]) writeToFile:Pathimg atomically:YES];
    NSLog(@"%@",path);
    
}
- (void)openSystemSet{
    NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    if([[UIApplication sharedApplication] canOpenURL:url]) {
        NSURL*url =[NSURL URLWithString:UIApplicationOpenSettingsURLString];
        [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
    } else {
        NSLog(@"无法打开设置");
    }
}

- (void)noti:(NSNotification *)center{
    NSDictionary *info = center.userInfo;
    [self.images addObjectsFromArray:info[@"image"]];
    
    self.image1.image = [self.images firstObject];
    self.image2.image = [self.images lastObject];
    
    NSString * path =NSHomeDirectory();
    NSString * Pathimg =[path stringByAppendingString:@"/Documents/111.png"];
    [UIImagePNGRepresentation([UIImage imageNamed:@"choose"]) writeToFile:Pathimg atomically:YES];
    

    
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.images removeAllObjects];
    
    PhotoListController *vc = [[PhotoListController alloc]init];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    [self presentViewController:nav animated:YES completion:nil];
    
}

- (NSMutableArray *)images{
    if (!_images) {
        _images = [NSMutableArray array];
    }
    return _images;
}

@end
